//pages/shanpin/shangpin.js
import {request} from "../../request/index"
Page({
  data: {
    tabs:[
      {
        id:0,
        value:"综合",
        isActive:true
      },{
        id:1,
        value:"销量",
        isActive:false
      },{
        id:2,
        value:"价格",
        isActive:false
      }
    ],
    shangpinList:[]
  },
  QueryParams:{
    query:"",
    cid:"",
    pagenum:1,
    pagesize:10
  },
  totalPages:1,
  onLoad: function (options) {
      this.QueryParams.cid = options.cid || "";
      this.QueryParams.query = options.query || ""
      this.getShangpinList()
  },
  async getShangpinList(){
    const res = await request({
      url:'/goods/search',
      data:this.QueryParams
    })
    const total = res.total;
    this.totalPages = Math.ceil(total/this.QueryParams);
    this.setData({
      shangpinList:[...this.data.shangpinList,...res.goods]
    })
    wx.stopPullDownRefresh()
  },
  handleTabsItemChange(e){
    const {index} = e.detail;
    let {tabs} = this.data;
    tabs.forEach((v,i)=>{
      i===index?v.isActive=true:v.isActive=false
    })
    this.setData({
      tabs
    })
  },
  onReachBottom(){
    if(this.QueryParams.pagenum>=this.totalPages){
      wx.showTabBar({
        title:"没有下一页数据"
      })
    }else{
      this.QueryParams.pagenum++;
      this.getShangpinList()
    }
  },
  onPullDownRefresh(){
    this.setData({
      shangpinList:[]
    })
    this.QueryParams.pagenum=1;
    this.getShangpinList()
  }
})
